import {
  listColumns,
} from './index.const';
import {
  fileDownload
} from '@const/utils.js';
import {
  $loadFileSimply
} from '@const/loadFileOps.js';
import {
  factorySelectAgentManager,
  factorySelectsaleTargetOrder,
  factorySelectAgentOrder,
  downloadNewsPaperData,
  downloadAllNewsPaperData
} from '@api/guoke_deepexi_dms_order_center_v1.js';
import ElEditable from '@components/el-editable';

export default {
  components: {
    ElEditable,
  },
  data() {
    return {
      // 多条件查询订单
      tableQuery: {
        saleTargetCode: '',
        companyId: '',
        saleStart: '',
        saleEnd: '',
        orderStatus: null,
        createStart: '',
        createEnd: '',
      },
      saleDate: ['', ''],
      createDate: ['', ''],
      tableLoading: false,
      listColumns: listColumns, //表头
      //列表数据
      payload: {},
      loading: false,
      //销售对象下拉
      saleTargetOptions: [],
      //经销商下拉
      companyNameOptions: [],
      // 主列表分页
      currentPage: 1,
      pageSize: 20,
      multipleSelection: []
    };
  },
  created() {
    this.getList();
  },
  mounted() {

  },
  methods: {

    // 重置
    queryFormReset() {
      this.tableQuery = {
        saleTargetCode: '',
        companyId: '',
        saleStart: '',
        saleEnd: '',
        orderStatus: null,
        createStart: '',
        createEnd: '',
      };
      this.saleDate = ['', '']
      this.createDate = ['', '']
      this.currentPage = 1;
      this.getList();
    },
    // 查询
    queryFormQuery() {
      this.currentPage = 1;
      this.getList();
    },
    //销售对象下拉
    saleTargetRemoteMethod(val) {
      if (val !== '') {
        this.loading = true;
        let query = {
          saleTargetName: val,
        };
        this.$axios.$post(factorySelectsaleTargetOrder, query).then(res => {
          this.saleTargetOptions = res.payload;
          this.loading = false;
        });
      }
    },
    // 经销商下拉
    companyNameRemoteMethod(val) {
      if (val !== '') {
        this.loading = true;
        let query = {
          agentName: val
        };
        this.$axios.$post(factorySelectAgentOrder, query).then(res => {
          this.companyNameOptions = res.payload;
          this.loading = false;
        });
      }
    },
    // 主列表数据
    getList() {
      this.tableLoading = true;
      const query = {
        ...this.tableQuery,
        page: this.currentPage,
        pageSize: this.pageSize,
        createStart: this.createDate ? this.createDate[0] : '',
        createEnd: this.createDate ? this.createDate[1] : '',
        saleStart: this.saleDate ? this.saleDate[0] : '',
        saleEnd: this.saleDate ? this.saleDate[1] : '',
      };
      this.$axios.$post(factorySelectAgentManager, query).then(res => {
        if (res.code === '0') {
          res.payload.content = res.payload.content || []
          this.payload = res.payload;
        }
        this.tableLoading = false;
      });
    },
    // 主列表分页器
    reloadProductData(val) {
      this.currentPage = val.number
      this.pageSize = val.size
      this.getList();

    },
    // 导出
    exportEx() {
      if (this.multipleSelection.length > 0) {
        let dataArr = []
        this.multipleSelection.forEach(item => {
          dataArr.push(item.orderId)
        })
        $loadFileSimply({
            method: 'post',
            url: downloadNewsPaperData,
            timeout: '0',
            data: {
              idList: dataArr
            }
          }).then(res => {
            if (res.byteLength) {
              fileDownload(res, '报台明细.xlsx')
            } else {
              this.$message({
                message: '导出失败',
                type: 'warning'
              })
            }
          })
          .catch(err => {
            this.$message({
              message: '导出失败',
              type: 'warning'
            })
          })
      } else {
        this.$message({
          message: '请选择导出数据',
          type: 'warning'
        })
      }
    },
    exportExAll() {
      if (this.payload.content && this.payload.content.length > 0) {
        $loadFileSimply({
            method: 'post',
            url: downloadAllNewsPaperData,
            timeout: '0',
            data: {
              ...this.tableQuery,
              page: this.currentPage,
              pageSize: this.pageSize,
              createStart: this.createDate ? this.createDate[0] : '',
              createEnd: this.createDate ? this.createDate[1] : '',
              saleStart: this.saleDate ? this.saleDate[0] : '',
              saleEnd: this.saleDate ? this.saleDate[1] : '',
            }
          }).then(res => {
            if (res.byteLength) {
              fileDownload(res, '报台明细.xlsx')
            } else {
              this.$message({
                message: '导出失败',
                type: 'warning'
              })
            }
          })
          .catch(err => {
            this.$message({
              message: '导出失败',
              type: 'warning'
            })
          })
      } else {
        this.$message({
          message: '暂无数据可导出',
          type: 'warning'
        })
      }
    },
    //表格选择数据
    selectionChange(val) {
      this.multipleSelection = val
    },

    // /操作栏-详情
    checkDetail(scope) {
      this.$router.push({
        path: '/report-bills-management/factory/detail',
        query: {
          id: scope.orderId,
        }
      });
    }
  }
};